﻿using System.Text;
using WFC.BL.Interfaces.DomainModels;
using WFC.BL.Interfaces.Enumerations;

namespace WFC.DAL.StatementSources
{
    internal class InsertContentsStatementSource : StatementSourceBase 
    {
        internal InsertContentsStatementSource(CmsPageMode cmsPageMode, string contentData, int createdBy)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("INSERT `wfc`.`Contents` (`PageMode`,`HtmlContent`,`CreatedBy`,`CreateDttm`) ");
            stringBuilder.Append("VALUES (@PageMode,@HtmlContent,@CreatedBy,NOW()); ");
            stringBuilder.Append("SELECT LAST_INSERT_ID();");

            Sql = stringBuilder.ToString();

            Parameters.AddWithValue("@PageMode", (int)cmsPageMode);
            Parameters.AddWithValue("@HtmlContent", contentData);
            Parameters.AddWithValue("@CreatedBy", createdBy);
        }
    }

    internal class ReadByIdContentsStatementSource : StatementSourceBase
    {
        internal ReadByIdContentsStatementSource(int contentId)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("SELECT `Id`,`PageMode`,`HtmlContent`,`CreatedBy`,`CreateDttm`,`UpdatedBy`,`UpdateDttm` ");
            stringBuilder.Append("FROM `wfc`.`Contents` ");
            stringBuilder.Append("WHERE `Id`=@Id AND `PageMode`=@PageMode");

            Sql = stringBuilder.ToString();

            Parameters.AddWithValue("@Id", contentId);
            Parameters.AddWithValue("@PageMode", (int)CmsPageMode.Published);
        }
    }

    internal class UpdateContentsStatementSource : StatementSourceBase
    {
        internal UpdateContentsStatementSource(IContentMaintainable content)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("UPDATE `wfc`.`Contents` SET `PageMode`=@PageMode,`HtmlContent`=@HtmlContent,`UpdateBy`=@UpdatedBy,`UpdateDttm`=NOW() ");
            stringBuilder.Append("WHERE `Id`=@Id");

            Sql = stringBuilder.ToString();

            Parameters.AddWithValue("@Id", content.Id);
            Parameters.AddWithValue("@PageMode", (int)content.PageMode);
            Parameters.AddWithValue("@HtmlContent", content.HtmlContent);
            Parameters.AddWithValue("@UpdatedBy", content.UpdatedBy);
        }
    }

    internal class DeleteContentsStatementSource : StatementSourceBase
    {
        internal DeleteContentsStatementSource(int contentId)
        {
            Sql = "DELETE FROM `wfc`.`Contents` WHERE `Id`=@Id";

            Parameters.AddWithValue("@Id", contentId);
        }
    }
}
